import codecs
import sys, os, torch

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../")
from datasets.car_lp_det.LpxmlContainer import MetaLpContainer

if __name__ == '__main__':
    con = MetaLpContainer()

    out_root_path = "/home/data/782_yololabel/"
    for pic_stru in con.all_pic_strus:
        txtname = os.path.join(out_root_path, pic_stru.file_name.replace(".jpg", ".txt"))
        with codecs.open(txtname, 'w', 'GBK') as f:
            for car in pic_stru.cars:
                x1, y1, x2, y2 = car.car_coor
                x1 = x1 / pic_stru.width
                y1 = y1 / pic_stru.height
                x2 = x2 / pic_stru.width
                y2 = y2 / pic_stru.height
                label = None
                if car.lp_pts is None:
                    label = 0
                    px1, py1 = 0, 0
                    px2, py2 = 0, 0
                    px3, py3 = 0, 0
                    px4, py4 = 0, 0
                else:
                    label = 1
                    px1, py1 = car.relative_lp_pts[0][0], car.relative_lp_pts[0][1]
                    px2, py2 = car.relative_lp_pts[1][0], car.relative_lp_pts[1][1]
                    px3, py3 = car.relative_lp_pts[2][0], car.relative_lp_pts[2][1]
                    px4, py4 = car.relative_lp_pts[3][0], car.relative_lp_pts[3][1]
                f.write('{} {} {} {} {} {} {} {} {} {} {} {} {}\n'.format(
                    label, (x1 + x2) / 2, (y1 + y2) / 2, abs(x1 - x2), abs(y1 - y2),
                    px1, py1,
                    px2, py2,
                    px3, py3,
                    px4, py4,
                ))
                # f.write('{} {} {} {} {} \n'.format(
                #     label, (x1 + x2) / 2, (y1 + y2) / 2, abs(x1 - x2), abs(y1 - y2)
                # ))
    print("END")
